﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VisionarySystems.Library;
using System.Data.SqlClient;
using System.Globalization;
using VisionarySystems.TimeRegi.Controller;
using VisionarySystems.TimeRegi.GUI;
using VisionarySystems.TimeRegi.Model;
using VisionarySystems.TimeRegi.Model.TimePeriods;

namespace VisionarySystems.TimeRegi
{
	public partial class Form_TimeRegistration : Form
	{
		private readonly ITimeRegistrationController m_TimeRegistrationController;
		private readonly TimeRegistrationViewModel m_ViewModel;

		#region private members

		//private TimeRegistrationHandler m_TimeRegistrationHandler = null;

		private static int WM_QUERYENDSESSION = 0x11;

		bool FirstTime = true;
		private AutomaticRegistrationController m_AutomaticRegistrationController;

		#endregion

		#region Ctor

		public Form_TimeRegistration(ITimeRegistrationController TimeRegistrationController, TimeRegistrationViewModel ViewModel)
		{
			m_TimeRegistrationController = TimeRegistrationController;
			m_ViewModel = ViewModel;
			InitializeComponent();
		}

		#endregion

		public void OnTimeSlotChanged()
		{
			UpdateAllControls();
		}

		#region Private Methods

		#region Form Events

		private void Form1_Load(object sender, EventArgs e)
		{
			try
			{

				UpdateAllControls();
			}
			catch (SqlException)
			{
				this.Close();
			}
			Application.Idle += new EventHandler(Application_Idle);
		}

		private void Form1_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (m_TimeRegistrationController != null)
				m_TimeRegistrationController.ApplicationClosing();
		}

		private void Form_TimeRegistration_Resize(object sender, EventArgs e)
		{
			if (FormWindowState.Minimized == WindowState)
				Hide();
		}

		private void Form_TimeRegistration_Shown(object sender, EventArgs e)
		{
			if (FirstTime)
			{
				FirstTime = false;
				Hide();
			}

		}

		#endregion

		#region Controls enents

		private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
		{

		}

		private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
		{
			TreeNode node = TreeOverview.SelectedNode;
			if (node != null)
			{
				int removedIndexCount = m_TimeRegistrationController.DeleteTimePeriode(node.Tag as TimePeriodeID);
				if (removedIndexCount > 0)
				{
					MessageBox.Show(string.Format("Deleted {0} TimeSlots", removedIndexCount));
					UpdateAllControls();
				}
				else
					MessageBox.Show(string.Format("Unable to delete Timeperiode"));
			}
			else
				MessageBox.Show(string.Format("Please select a Time periode"));
		}

		/// <summary>
		/// Show the form
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void notifyIcon1_DoubleClick(object sender, EventArgs e)
		{
			Show();
			WindowState = FormWindowState.Normal;
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			if (timer1.Interval != 60000)
				timer1.Interval = 60000;
			//notifyIcon1.Text = m_TimeRegistrationHandler.GetSummary();
			notifyIcon1.Text = m_ViewModel.GetSummary();
		}

		/// <summary>
		/// Update the hint in the Tray bar
		/// </summary>
		private void toolStripMenuItem1_Click(object sender, EventArgs e)
		{
			this.Close();
		}
		#endregion
		
		#region system methods

		/// <summary>
		/// Hide the form at startup
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Application_Idle(object sender, EventArgs e)
		{
			//it then uses P/Invoke to get access to Windows API message handling routines.

		}

		protected override void WndProc(ref System.Windows.Forms.Message m)
		{
			if (m.Msg == WM_QUERYENDSESSION)
			{
				//MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot");
				m_TimeRegistrationController.ShutDown();
				//systemShutdown = true;
			}

			// If this is WM_QUERYENDSESSION, the closing event should be
			// raised in the base WndProc.
			base.WndProc(ref m);

		}
		
		#endregion

		private void UpdateAllControls()
		{
			tb_Overview.Text = m_ViewModel.GetOverview();
			UpdateTreeOverview();
			notifyIcon1.Text = m_ViewModel.GetSummary();
		}

		private void pauseToolStripMenuItem_Click(object sender, EventArgs e)
		{
			m_TimeRegistrationController.StartPause();
		}

		private void Assemble_Click(object sender, EventArgs e)
		{
			TreeNode node = TreeOverview.SelectedNode;
			if (node != null)
			{
				int removedIndexCount = m_TimeRegistrationController.AssembleTimeslots(node.Tag as TimePeriodeID);
				if (removedIndexCount > 0)
				{
					MessageBox.Show(string.Format("Assembled {0} TimeSlots", removedIndexCount));
					UpdateAllControls();
				}
				else
					MessageBox.Show(string.Format("Unable to assemble TimeSlots"));
			}
			else
				MessageBox.Show(string.Format("Please select a Time periode"));
		}

		private void UpdateTreeOverview()
		{
			TreeOverview.Nodes.Clear();

			TreeNode node;
			foreach (ITimePeriode periode in m_ViewModel.TimePeriodModel)
			{
				node = periode.GetNode();
				TreeOverview.Nodes.Add(node);
			}
		}


		//private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
		//{
		//	TreeNode node = TreeOverview.SelectedNode;
		//	if (node != null)
		//	{

		//		int removedIndexCount = m_TimeRegistrationHandler.DeleteTimePeriode(node.Tag as TimePeriodeID);
		//		if (removedIndexCount > 0)
		//		{
		//			MessageBox.Show(string.Format("Deleted {0} TimeSlots", removedIndexCount));
		//			UpdateAllControls();
		//		}
		//		else
		//			MessageBox.Show(string.Format("Unable to delete Timeperiode"));
		//	}
		//	else
		//		MessageBox.Show(string.Format("Please select a Time periode"));
		//}

		private void ChangeTimeSlot(TimeSlot Slot)
		{
			Form_TimeSlotEdit editForm = new Form_TimeSlotEdit();
			editForm.Fill(Slot);
			if (editForm.ShowDialog() == DialogResult.OK)
			{
				m_TimeRegistrationController.UpdateTimeSlot(editForm.EditedTimeSlot);
				UpdateAllControls();
			}
		}

		private void editToolStripMenuItem_Click(object sender, EventArgs e)
		{
			TreeNode node = TreeOverview.SelectedNode;
			if (node != null)
			{
				TimePeriodeID id = node.Tag as TimePeriodeID;
				TimeSlot editTimeSlot = m_ViewModel.GetTimePeriode(id) as TimeSlot;
				if (editTimeSlot != null)
				{
					ChangeTimeSlot(editTimeSlot);
				}
				else
					MessageBox.Show(string.Format("Please select a Time slot"));
			}
			else
				MessageBox.Show(string.Format("Please select a Time slot"));
		}

		private void addTimeslotToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ChangeTimeSlot(null);
		}

		#endregion

		private void opdaterToolStripMenuItem_Click(object sender, EventArgs e)
		{
			m_ViewModel.Update();
			UpdateAllControls();
		}
	}
}
